<?php


namespace app\home\controller;


use app\admin\model\StoreModel;
use app\home\services\CityService;
use app\home\services\StoreService;
use app\utils\Response;

class StoreController extends ApiBase
{
    /**
     * 增加
     */
    public function save()
    {
        $storeService = new StoreService();
        if (!isset($this->requestData['id'])) {
            Response::fail("id必传");
        }
        $descImgs = [];
        if (!empty($this->requestData['desc_imgs'])) {
            $descImgs = $this->requestData['desc_imgs'];
            foreach ($descImgs as &$descImg) {
                $descImg = str_replace(env('DOMAIN_PATH'), '', $descImg);
            }
        }
        $storeData = [
            'member_id' => $this->memberInfo['id'],
            'province_id' => $this->requestData['province_id'],
            'city_id' => $this->requestData['city_id'],
            'area_id' => $this->requestData['area_id'],
            'title' => $this->requestData['store_title'],
            'link_mobile' => $this->requestData['store_mobile'],
            'address' => $this->requestData['store_address'],
            'link_name' => $this->requestData['store_name'],
            'desc' => $this->requestData['desc'],
            'desc_imgs' => json_encode($descImgs, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
            'add_time' => time(),
        ];
        $this->requestData['title'] = $this->requestData['store_title'];
        [$code, $msg] = $storeService->save($this->requestData['id'], $storeData);
        if ($code != 0) {
            Response::fail($msg);
        }
        Response::success([], $msg);
    }

    /**
     * 根据省、市、关键字搜索拳馆列表
     */
    public function searchStoreList()
    {
        $storeService = new StoreService();
        if (!isset($this->requestData['province_id'])) {
            Response::fail("省id必传");
        }
        if (!isset($this->requestData['city_id'])) {
            Response::fail("市id必传");
        }
        $searchParam = [
            'province_id' => $this->requestData['province_id'],
            'city_id' => $this->requestData['city_id'],
            'page' => $this->requestData['page'],
            'pageSize' => $this->requestData['page_size'],
            'title' => $this->requestData['title']
        ];
        $list = $storeService->getListByWhere($searchParam, ['id', 'title', 'address']);
        Response::success([
            'store_list' => $list
        ]);
    }

    /**
     * 馆长获取自己的道馆列表
     */
    public function curatorGetStoreList()
    {
        $cityService = new CityService();
        $storeModel = new StoreModel();
        if ($this->memberInfo['identity'] != 4) {
            Response::fail("您的身份不是馆长");
        }
        $list = $storeModel->where([
            'status' => 1,
            'is_delete' => 1,
            'member_id' => $this->memberInfo['id']
        ])->select();
        $return = [];
        foreach ($list as &$item) {
            $item['province_zh'] = "";
            $item['city_zh'] = "";
            $item['area_zh'] = "";
            $cityIds = [$item['province_id'], $item['city_id'], $item['area_id']];
            $cityList = $cityService->getDataByIds($cityIds, ['cid', 'title', 'level']);
            foreach ($cityList as $city) {
                if ($city['level'] == 1) {
                    $item['province_zh'] = $city['title'];
                } else if ($city['level'] == 2) {
                    $item['city_zh'] = $city['title'];
                } else if ($city['level'] == 3) {
                    $item['area_zh'] = $city['title'];
                }
            }
            $return[] = [
                'id' => $item['id'],
                'title' => $item['title'],
                'link_mobile' => $item['link_mobile'],
                'link_name' => $item['link_name'],
                'province_id' => $item['province_id'],
                'city_id' => $item['city_id'],
                'area_id' => $item['area_id'],
                'address' => $item['address'],
                'province_zh' => $item['province_zh'],
                'city_zh' => $item['city_zh'],
                'area_zh' => $item['area_zh'],
            ];
        }
        Response::success([
            'store_list' => $return
        ]);
    }

    /**
     * 获取道馆详情
     */
    public function getInfo()
    {
        $cityService = new CityService();
        $storeService = new StoreService();
        if ((int)$this->requestData['id'] <= 0) {
            Response::fail("道馆id异常");
        }
        $storeId = (int)$this->requestData['id'];
        $storeInfo = $storeService->getInfoById($storeId, ['id', 'link_mobile', 'title', 'address', 'desc', 'desc_imgs', 'province_id', 'city_id', 'area_id']);
        $descImgs = json_decode($storeInfo['desc_imgs'], true);
        foreach ($descImgs as &$desc_img) {
            $desc_img = env('DOMAIN_PATH') . $desc_img;
        }
        $storeInfo['desc_imgs'] = $descImgs;
        $cityIds = [$storeInfo['province_id'], $storeInfo['city_id'], $storeInfo['area_id']];
        $cityList = $cityService->getDataByIds($cityIds, ['cid', 'title'])->toArray();
        $cityList = array_column($cityList, null, 'cid');
        $storeInfo['province_zh'] = $cityList[$storeInfo['province_id']]['title'];
        $storeInfo['city_zh'] = $cityList[$storeInfo['city_id']]['title'];
        $storeInfo['area_zh'] = $cityList[$storeInfo['area_id']]['title'];
        Response::success([
            'store_info' => $storeInfo
        ]);
    }
}